$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'noc/noctemplate/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '模板名称', name: 'template', index: 'template', width: 150 },
            { label: '头图pc', name: 'headImgPc', index: 'head_img_pc', width: 450 },
            { label: '头图', name: 'headImg', index: 'head_img', width: 450 },
            { label: '底图pc', name: 'footImgPc', index: 'foot_img_pc', width: 450 },
            { label: '底图', name: 'footImg', index: 'foot_img', width: 450 },
            { label: '主色', name: 'colorBg', index: 'color_bg', width: 100 },
            { label: '联系电话', name: 'contactTel', index: 'contact_tel', width: 100 },
            { label: '链接', name: 'url', index: 'url', width: 300 },
            { label: '是否', name: 'isDefault', index: 'is_default', width: 300,hidden:true},
            { label: '短信账号(阿里云)', name: 'msgAccout', index: 'msg_accout', width: 120},
            { label: '短信密码', name: 'msgPwd', index: 'msg_pwd', width: 80},
            { label: '短信账号(优讯通)', name: 'msgyxAccount', index: 'msgyx_account', width: 120},
            { label: '短信密码', name: 'msgyxPwd', index: 'msgyx_pwd', width: 80},
            { label: '头条key', name: 'toutiaoKey', index: 'toutiao_key', width: 150},
            { label: '头条token', name: 'toutiaoToken', index: 'toutiao_token', width: 150},
            { label: 'appId', name: 'appid', index: 'appid', width: 150},
            { label: 'appSecret', name: 'appsecret', index: 'appsecret', width: 150},
            { label: 'tongxing', name: 'tongxing', index: 'tongxing', width: 150},
            { label: 'guanggao', name: 'guanggao', index: 'guanggao', width: 150},
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:false,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
        loadComplete: function(data){
            //设置主活动
            var obj = $("#jqGrid");
            var rowIds = obj.getDataIDs();
            if (rowIds.length > 0) {
                for (var i = 0; i < rowIds.length; i++) {
                    console.log(obj.getRowData(rowIds[i]));
                    var rowData = obj.getRowData(rowIds[i]);
                    if(rowData.isDefault=="1"){
                        obj.setSelection(rowIds[i]);
                    }
                }
            }
        },
        beforeSelectRow: beforeSelectRow,//js方法
    });
});

//单选设置
function beforeSelectRow()
{
    $("#jqGrid").jqGrid('resetSelection');
    return(true);
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		nocTemplate: {},
        url: ""
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.nocTemplate = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.nocTemplate.id == null ? "noc/noctemplate/save" : "noc/noctemplate/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.nocTemplate),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "noc/noctemplate/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        isDefault: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "noc/noctemplate/isDefault/"+id, function(r){
                layer.msg("操作成功", {icon: 1});
            });
        },
		getInfo: function(id){
			$.get(baseURL + "noc/noctemplate/info/"+id, function(r){
                vm.nocTemplate = r.nocTemplate;
                var url = vm.getUrl(id);
                console.log(url);
                vm.nocTemplate["url"] = url;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		getUrl: function (id) {
            var port = location.port;
            var hostUrl = "http://";
            if(port == 80){
                hostUrl += location.hostname;
            }else{
                hostUrl += location.host;
            }
            //console.log(hostUrl);
            return hostUrl+"/h5/index?tid="+id+"&ly=填写来源"
        },

}
});